package com.sup.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sup.beans.ProductionBean;
import com.sup.beans.ProductionIdBean;
import com.sup.beans.SaleLineItemBean;
import com.sup.common.Json;
import com.sup.common.Message;
import com.sup.model.Discount;
import com.sup.model.Production;
import com.sup.model.Production;
import com.sup.model.ProductionType;
import com.sup.service.DiscountService;
import com.sup.service.ProductionService;
import com.sup.service.ProductionTypeService;

/**
 * 
 * @author SKH
 *
 */
@Controller
@RequestMapping(value = "/production")
public class ProductionController {
	public final static Logger logger = LoggerFactory
			.getLogger(ProductionController.class);
	@Autowired
	ProductionService productionService;
	@Autowired
	ProductionTypeService typeService;
	@Autowired
	DiscountService discountService;
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String saveProduction(ProductionBean productionBean) {
		Json json = new Json();
		Production production = getProductionByProductionBean(productionBean);
		productionService.saveProduction(production);
		json.setMsg("添加产品成功");
		json.setSuccess(true);
		logger.info("商品名为：" + production.getProductionName() + " 保存到数据库");
		return new Message(json).writeJson();
	}

	@RequestMapping(value = "/update")
	@ResponseBody
	public String updateProduction(ProductionBean productionBean) {
		Json json = new Json();
		Production production = getProductionByProductionBean(productionBean);
		Production Production2 = productionService
				.findProductionById(production.getId());
		if (Production2.compareTo(production)) {
			productionService.saveProduction(production);
			json.setSuccess(true);
			json.setMsg("修改用户成功");
		} else {
			json.setMsg("修改失败");
		}
		return new Message(json).writeJson();
	}

	@RequestMapping(value = "/delete")
	@ResponseBody
	public String deleteProduction(Long id) {
		Json json = new Json();
		productionService.removeProduction(id);
		json.setSuccess(true);
		json.setMsg("删除产品成功");
		return new Message(json).writeJson();
	}

	/**
	 * 得到数据列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public String grid(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows") Integer rows) {

		int pageIndex = page - 1;
		Page<Production> productionPage = productionService.getProductionPage(
				pageIndex, rows);
		Iterator<Production> iterator = productionPage.iterator();
		List<ProductionBean> productionBeanList = new ArrayList<ProductionBean>();
		while (iterator.hasNext()) {
			Production production=iterator.next();
			ProductionType productionType=typeService.findProductionType(production.getProductionTypeId());
			ProductionBean productionBean=new ProductionBean();
			productionBean.setId(production.getId());
			productionBean.setProductionTypeName(productionType.getProductionType());
			productionBean.setProductionName(production.getProductionName());
			productionBean.setDescription(production.getDescription());
			productionBean.setPrice(production.getPrice());
			productionBeanList.add(productionBean);
		}
		int total = (int) productionPage.getTotalElements();
		return new Message(total, productionBeanList).toJson();
	}

	public Production getProductionByProductionBean(
			ProductionBean productionBean) {
		Production production = new Production();
		production.setId(productionBean.getId());
		production.setProductionTypeId(Long.parseLong(productionBean.getProductionTypeName()));
		production.setProductionName(productionBean.getProductionName());
		production.setDescription(productionBean.getDescription());
		production.setPrice(productionBean.getPrice());
		return production;
	}
	
	@RequestMapping(value="/findAll")
	@ResponseBody
	public String getAllProductionId(){
		List<Production> productions=productionService.findAll();
		List<ProductionIdBean> productionIdBeans=new ArrayList<ProductionIdBean>();
		if(productions!=null){
			Iterator<Production> iterator=productions.iterator();
			while(iterator.hasNext()){
				Production production=iterator.next();
				ProductionIdBean productionIdBean=new ProductionIdBean();
				productionIdBean.setId(production.getId());
				productionIdBean.setProductionId(production.getId()+"");
				productionIdBeans.add(productionIdBean);
			}
		}
		return new Message(productionIdBeans).writeJson();
	}

	@RequestMapping(value="/find")
	@ResponseBody
	public String findProduction(Long productionId){
		Production production=productionService.findProductionById(productionId);
		Discount discount=discountService.finDiscountByProductionId(productionId);
		int percent=100;
		if(discount!=null){
			percent=discount.getPercent();
		}
		SaleLineItemBean saleLineItemBean=new SaleLineItemBean();
		saleLineItemBean.setId(productionId);
		saleLineItemBean.setProductionName(production.getProductionName());
		saleLineItemBean.setAmount(1);
		saleLineItemBean.setPercent(percent);
		saleLineItemBean.setPrice(production.getPrice());
		return new Message(saleLineItemBean).writeJson();
	}
}
